from django.shortcuts import render, get_object_or_404
from django.http import *
from django.db.models import Q
from django.core.exceptions import ObjectDoesNotExist
from django.template import RequestContext, loader
from models import Event
from django.contrib.auth.models import User
import urllib
import urllib2
import cgi

# Create your views here.
def index(request):
	if request.user.is_authenticated():
		return render(request, 'events/index.html')	
	else:
		return HttpResponseForbidden('Forbidden')

def detail(request, event_id):
	if request.user.is_authenticated():
		event = get_object_or_404(Event, id=event_id)
		return render(request, 'events/detail.html', {"event" : event})
	else:
		return HttpResponseForbidden('Forbidden')
		
def create(request):
	if request.user.is_authenticated():
		return render(request, 'events/create.html')
	else:
		return HttpResponseForbidden('Forbidden')
	
def list(request):
	if request.user.is_authenticated():
		return render(request, 'events/list.html', {"events" : Event.objects.all()})
	else:
		return HttpResponseForbidden('Forbidden')
		
def export(request):
	if request.user.is_authenticated():
		return render(request, 'events/export.xml', {"events" : Event.objects.all()})
	else:
		return HttpResponseForbidden('Forbidden')

def calendar(request):
	if request.user.is_authenticated():
		return render(request, 'events/calendar.html')
	else:
		return HttpResponseForbidden('Forbidden')
		
def create_app_event(event):
	try:
		Event.objects.create(name = event.name, start_time = event.start_time, end_time = event.end_time, location = event.location, description = event.description)
		return "Event added to database"
	except Exception, e:
		return e

def create_facebook_event(event):
	# Get Facebook Graph access token for our Page
	response = cgi.parse_qs(urllib.urlopen('https://graph.facebook.com/oauth/access_token?client_id=140714289449035&client_secret=c410f123455bacdd6cbab8ca1e525108&grant_type=client_credentials').read())
	access_token = response["access_token"][-1]
	
	# TODO: Create valid post data
	event = Event
	post_data = {
		'access_token' : access_token,
		'name' : event.name,
		# HACK: Add time zone to date so it is formatted correctly
		'start_time' : event.start_time + "-05:00",
		'end_time' : event.end_time + "-05:00",
		'location' : event.location,
		'description' : event.description,
	}
	
	# POST data to our Page's events
	response = urllib.urlopen('https://graph.facebook.com/425606474201337/events', urllib.urlencode(post_data))
	content = response.read()
	
	return content
	
def create_google_event(event):
	# TODO: POST to Google: https://www.googleapis.com/calendar/v3/calendars/studentorganization313@gmail.com/events
	# See https://developers.google.com/google-apps/calendar/v3/reference/events/insert
	# Need to URL-encode calendar id?
	return "Did not send to Google"
	
def do_create(request):
	if request.user.is_authenticated() and request.method == 'POST':
		event = Event
		event.name = request.POST['name']
		event.start_time = request.POST['start_time']
		event.end_time = request.POST['end_time']
		event.location = request.POST['location']
		event.description = request.POST['description']
		
		app_response = create_app_event(event)
		fb_response = create_facebook_event(event)
		g_response = create_google_event(event)
		
		result = ""
		
		result = result + "App response: " + app_response + "<br/>"
		result = result + "Facebook response: " + fb_response + "<br/>"
		result = result + "Google response: " + g_response + "<br/>"
		
		return render(request, 'events/on_create.html', {"app_response" : app_response, "fb_response" : fb_response, "g_response" : g_response})
	else:
		return HttpResponseForbidden('Forbidden')
